

import { FighterModel } from "core/model/fighterModel"
import { PartnerModel } from "core/model/partnerModel"
import { PlayerModel } from "core/model/playerModel"
import fighterHandle from "./fighterHandle"
import { FighterTeamInfoModel } from "core/model/battleInfoModel"
import { gameConfigHandle, propName } from "core/config/gameConfig"
import utils from "core/utils/utils"
import { ItemModel } from "core/model/itemModel"
import { AttriType, EquipmentType, MapType, UnitType } from "core/typeDefine/typeDefine"
import partnerHandle from "./partnerHandle"
import { FighterSkillModel } from "core/model/skillModel"
import { TreasureItemModel } from "core/model/treasureModel"
import petHandle from "./petHandle"
import playerHandle from "./playerHandle"
import equipmentHandle from "./equipmentHandel"
import skillHandle from "./skillHandle"
import spiritHandle from "./spiritHandle"


class FighterPartnerHandle {
    initPartnerFighterTeam(player: PlayerModel, strictMode = false, mapType?: MapType, isMonthVip?: boolean, isYearVip?: boolean): FighterTeamInfoModel {
        let team1: FighterModel[] = new Array(6)
        let fighterTeamInfo = new FighterTeamInfoModel()
        let altarFighter = this.createAltarFighter(player)
        fighterTeamInfo.altar = altarFighter
        let rate = {
            poisonRate: 0, antiPoisonRate: 0, bleedRate: 0, antiBleedRate: 0, stunRate: 0, antiStunRate: 0,
            freezeRate: 0, antiFreezeRate: 0, silentRate: 0, antiSilentRate: 0, burnRate: 0, antiBurnRate: 0,
            controlRate: 0, antiControlRate: 0
        }
        // mapType = MapType.Front//测试用
        let teamList = player.team.teamList
        if (!mapType) {
            mapType = MapType.Normal
        }
        if (mapType == MapType.Front) {
            teamList = player.team.frontTeamList
            // console.log(teamList)
        }
        for (let i = 0; i < teamList.length; i++) {
            let tid = teamList[i]

            if (tid) {
                if (mapType !== MapType.Front || (mapType == MapType.Front && !player.luck.front.deadHeros.includes(tid))) {
                    let partner = playerHandle.getPartnerByIndexID(player, tid, mapType == MapType.Front)
                    let partnerFighter = this.initPartnerFighter(player, partner, i, altarFighter, strictMode, mapType, isMonthVip, isYearVip)

                    team1[i] = partnerFighter

                    utils.keys(rate).map(key => {
                        rate[key] += partnerFighter.battleAttris[key]
                    })
                }
            }
        }

        utils.keys(rate).map(key => {
            fighterTeamInfo.altar.attris[key] = rate[key] / 5
        })
        fighterHandle.calcBattleFighterAttri(altarFighter)
        altarFighter.battleAttris.hp = altarFighter.battleAttris.maxHp

        fighterTeamInfo.team = team1
        fighterTeamInfo.battleSpeed = player.team.battleSpeed.slice(0)

        return fighterTeamInfo
    }

    initPartnerFighter(player: PlayerModel, partner: PartnerModel, battlePos: number, altarFighter?: FighterModel,
        strictMode = false, mapType = MapType.Normal, isMonthVip = false, isYearVip = false) {
        if (!altarFighter) {
            altarFighter = this.createAltarFighter(player)
        }
        let fighter = new FighterModel()
        let partnerItemConfig = gameConfigHandle.configByID[partner.tid]
        fighter.tid = partner.tid
        fighter.lvl = partner.lvl
        fighter.quality = partnerItemConfig.quality
        if (partnerItemConfig.quality == 2) {
            fighter.quality = 3
        }
        this.calcInitAttri(player, partner, fighter)
        this.calcPartnerAttri(player, partner, fighter)

        this.calcEquipments(player, partner, fighter)
        // this.calcGemTeamAttris(player, partner, fighter)
        this.calcTreasure(player, partner, fighter, battlePos)
        this.calcTalent(player, partner, fighter, battlePos)

        this.calcImmune(player, partner, fighter)
        // this.calcVaccine(player, partner, fighter)
        this.calcPet(player, partner, fighter, battlePos)
        this.calcGodAttris(player, partner, fighter)//神灵属性
        this.calcFriendshipAttris(player, partner, fighter)//结交属性
        this.calcSpiritAttris(player, partner, fighter)//灵器属性
        if (mapType == MapType.WBoss) {
            this.calcMotivateAttris(player, fighter)
        }
        this.calcSkill(player, partner, fighter)
        this.calcVip(player, partner, fighter, isMonthVip, isYearVip)
        // this.calcMagicMachine(player, partner, fighter)
        this.calcFighterAltarAttris(player, partner, fighter, altarFighter)
        this.calcBaseAttri(player, partner, fighter)
        fighterHandle.calcSkillsAttris(fighter)
        this.calcFormationAttris(player, fighter, mapType)
        fighterHandle.calcBaseAttrisFighterAttri(fighter)

        // fighter.attris.anger = (fighter.attris.startMp || 0)

        if (strictMode) {
            fighter.attrisStr = JSON.stringify(fighter.attris)
        }
        fighterHandle.calcBattleFighterAttri(fighter)
        fighter.battleAttris.hp = fighter.battleAttris.maxHp
        fighter.battleAttris.mp = fighter.battleAttris.maxMp
        fighter.battleAttris.anger = (fighter.attris.startMp || 0)
        // fighter.shield = Math.floor(fighter.battleAttris.def * 0.2 * 0.5) //初始护盾值为最大护盾值的50%
        fighterHandle.caclPower(fighter)
        return fighter
    }

    //灵器属性
    calcSpiritAttris(player: PlayerModel, partner: PartnerModel, fighter: FighterModel) {
        let partnerItemConfig = gameConfigHandle.configByID[partner.tid]
        let occType = partnerItemConfig.occType
        let spirit = player.spirit
        let spiritConfig = gameConfigHandle.gameConfig.spirit
        let attriLvlupConfig = gameConfigHandle.gameConfig.attriLvlup
        spiritConfig.spiritLists[occType].forEach((id, i) => {
            let lvl = spirit.spiritLvls[id] || 0
            if (lvl > 0) {
                let itemConfig = gameConfigHandle.configByID[id]
                itemConfig.attris.forEach((attri, i) => {
                    let value = partnerHandle.getAttriValues(attri, lvl, attriLvlupConfig.spiritAttriValues[i])
                    // console.log(itemConfig.name, attri, value)
                    fighter.attris[attri] += value
                })
            }
        })
        let totalLvl = spiritHandle.getOccSpiritLvlTotal(player, occType)
        spirit.totalLvlAttris[occType] && spirit.totalLvlAttris[occType].forEach(attri => {
            let value = partnerHandle.getAttriValues(attri, totalLvl, attriLvlupConfig.spiritTotalLvlAttriValues)
            fighter.attris[attri] += value
        })
    }

    //结交属性
    calcFriendshipAttris(player: PlayerModel, partner: PartnerModel, fighter: FighterModel) {
        let partnerItemConfig = gameConfigHandle.configByID[partner.tid]
        let friendship = player.friendship
        let friendshipConfig = gameConfigHandle.gameConfig.friendship
        let attriLvlupConfig = gameConfigHandle.gameConfig.attriLvlup
        // console.log(gameConfigHandle.propName[partner.tid])
        friendshipConfig.lists.forEach((list, i) => {
            list.forEach(id => {
                let lvl = friendship.friendshipLvls[id] || 0
                let itemConfig = gameConfigHandle.configByID[id]
                if (itemConfig && partnerItemConfig.occType == itemConfig.occType && lvl > 0) {
                    let attris = friendship.attris[id]
                    if (attris) {
                        attris.forEach(attriName => {
                            let value = partnerHandle.getAttriValues(attriName, lvl, attriLvlupConfig.friendshipAttriCoe[i])
                            // console.log(1,attriName,value)
                            fighter.attris[attriName] += value
                        })
                    }
                    let subAttris = friendship.subAttris[id]
                    if (subAttris) {
                        subAttris.forEach(attriName => {
                            let value = partnerHandle.getAttriValues(attriName, lvl, attriLvlupConfig.friendshipSubAttriCoe[i])
                            // console.log(2,attriName,value)
                            fighter.attris[attriName] += value
                        })
                    }
                }
            })
        })
    }

    //神灵属性
    calcGodAttris(player: PlayerModel, partner: PartnerModel, fighter: FighterModel) {
        let partnerItemConfig = gameConfigHandle.configByID[partner.tid]
        let god = player.god
        let talentConfig = gameConfigHandle.gameConfig.talent
        let attriValuesConfig = gameConfigHandle.gameConfig.attriLvlup.godAttriValues
        gameConfigHandle.gameConfig.talent.godIndexs.forEach(i => {
            if (god.list[god.activeIndex]) {
                let lvl = god.nodeLvls[i] || 0
                let node = god.list[god.activeIndex][i]
                let attriNames = node.attriNames
                let unlock = false
                let nodeTypeLvl = talentConfig.godNodeTypeLvl[i]
                if (nodeTypeLvl == 0 || god.unlockNodes.includes(i)) {
                    unlock = true
                }
                if (unlock && lvl > 0 && node.type == partnerItemConfig.occType) {
                    // let nodeTypeLvl = talentConfig.godNodeTypeLvl[i]
                    attriNames.forEach((attri, j) => {
                        if (attri == 'recoverPer') {
                            attri = 'recoverCurePer'
                        }
                        let value = partnerHandle.getAttriValues(attri, lvl, attriValuesConfig[j])
                        fighter.attris[attri] += value
                    })
                }
            }
        })
    }

    //世界boss激励属性
    calcMotivateAttris(player: PlayerModel, fighter: FighterModel) {
        let motivateLvl = player.adventure.bossMapData.motivateLvl
        let mapConfig = gameConfigHandle.gameConfig.map
        let value = motivateLvl * mapConfig.motivateLvlUpPer
        mapConfig.motivateAttris.forEach(attriName => {

            fighter.attris[attriName] += value
        })
    }

    calcFormationAttris(player: PlayerModel, fighter: FighterModel, mapType: MapType) {
        let formationConfig = gameConfigHandle.gameConfig.formation
        let formationIndex = player.formation.formationList.find(f => f != null)
        if (formationIndex != null) {
            let formationLvl = player.formation.formationLvl[formationIndex] || 0
            let formationAttris = formationConfig.formationAttris[formationIndex]
            if (formationAttris != null) {
                // 阵型位置
                let pos = player.team.teamList.findIndex(tid => tid == fighter.tid)
                if (mapType == MapType.Front) {
                    pos = player.team.frontTeamList.findIndex(tid => tid == fighter.tid)
                    // console.log(pos, fighter.tid, propName[fighter.tid])
                }
                let attris
                if (pos < 5 && pos >= 0) {
                    attris = formationAttris.front
                } else if (pos >= 5 && pos < 10) {
                    attris = formationAttris.back
                }
                attris.forEach(attriName => {
                    let isPer = utils.isPerByAttri(attriName)
                    let formationAttriCoe = gameConfigHandle.gameConfig.attriLvlup.formationAttriCoe[isPer ? 1 : 0]
                    let value = partnerHandle.getAttriValues(attriName, formationLvl, formationAttriCoe)
                    let afterValue = fighter.attris[attriName] + value
                    fighter.attris[attriName] = afterValue > 0 ? afterValue : 0
                })
            }
        }
    }

    createAltarFighter(player: PlayerModel) {
        let fighter = new FighterModel()
        fighter.lvl = player.altar.lvl
        fighter.unitType = UnitType.Altar
        fighter.tid = player.altar.tid
        this.calcAltarAttri(player, fighter)
        this.calcAltarSkills(player, fighter)
        this.calcAltarAttriStr(player, fighter)
        fighterHandle.calcBattleFighterAttri(fighter)
        fighter.battleAttris.hp = fighter.battleAttris.maxHp
        return fighter
    }

    calcAltarAttriStr(player: PlayerModel, fighter: FighterModel) {
        let gameConfig = gameConfigHandle.gameConfig
        let attriLvls = player.altar.attriLvls
        let attriCoes = gameConfigHandle.gameConfig.attriLvlup.alterStrAttriValues
        gameConfigHandle.gameConfig.altar.attriTypeList.forEach((attriItemConfig, i) => {
            let lvl = attriLvls[i] || 0
            let attriCoes = gameConfig.attriLvlup.alterStrAttriValues[i] || []
            attriItemConfig.attris.forEach((attriName, j) => {
                let value = partnerHandle.getAltarAttriValues(attriName, lvl, attriCoes[j])
                fighter.attris[attriName] = (fighter.attris[attriName] || 0) + value
            })

        })
    }

    calcAltarAttri(player: PlayerModel, fighter: FighterModel) {
        let altarConfig = gameConfigHandle.gameConfig.altar
        let partnerConfig = gameConfigHandle.gameConfig.partner
        utils.entries(altarConfig.baseAttri).map(attriItem => {
            let attriName = attriItem[0]
            let attriValue = attriItem[1]
            fighter.attris[attriName] += attriValue
        })
        let starLvl = partnerHandle.getPartnerStarLvl(fighter.lvl)
        utils.entries(altarConfig.lvlupAttris).map(attriItem => {
            let attriName = attriItem[0]
            let attriValues = attriItem[1]
            if (Object.keys(altarConfig.baseAttri).indexOf(attriName) == -1) {
                return
            }
            let value = utils.getIncValues(attriValues, fighter.lvl)
            fighter.attris[attriName] += value + value * starLvl * partnerConfig.starLvlupAttriPer / 100
        })

        let treasureConfig = gameConfigHandle.gameConfig.treasure
        let attriLvlupConfig = gameConfigHandle.gameConfig.attriLvlup
        treasureConfig.list.map(treasureid => {
            let treasureItemInfo: TreasureItemModel = player.treasure.list[treasureid]
            if (treasureItemInfo && treasureItemInfo.lvl > 0) {
                let treasureItemConfig = gameConfigHandle.configByID[treasureid]
                treasureItemConfig.attris.map((attriItem, i, array) => {
                    let attriName = attriItem.attriName
                    let attriType = attriItem.attriType
                    if (attriType == AttriType.AltarAttri) {
                        let value = partnerHandle.getAttriValues(attriName, treasureItemInfo.lvl, attriLvlupConfig.treasureAttriCoe)
                        fighter.attris[attriName] += value
                    }
                })
            }

        })

    }

    calcInitAttri(player: PlayerModel, partner: PartnerModel, fighter: FighterModel) {
        let partnerConfig = gameConfigHandle.gameConfig.partner
        let attriLvlupConfig = gameConfigHandle.gameConfig.attriLvlup
        utils.entries(partnerConfig.initAttris).map(attriItem => {
            let attriName = attriItem[0]
            let attriValue = attriItem[1]
            fighter.attris[attriName] += attriValue
        })

    }


    calcPartnerAttri(player: PlayerModel, partner: PartnerModel, fighter: FighterModel) {
        let partnerConfig = gameConfigHandle.gameConfig.partner
        let attriLvlupConfig = gameConfigHandle.gameConfig.attriLvlup

        let starLvl = partner.starLvl
        partnerConfig.lvlUpAttris.map(attriName => {
            let value = partnerHandle.getAttriValues(attriName, partner.lvl, attriLvlupConfig.partnerLvlUpCoe)
            fighter.attris[attriName] += value + value * starLvl * partnerConfig.starLvlupAttriPer / 100
        })

    }

    calcFighterAltarAttris(player: PlayerModel, partner: PartnerModel, fighter: FighterModel, altarFighter: FighterModel) {
        let altarConfig = gameConfigHandle.gameConfig.altar
        altarFighter.skills.forEach((skill, i) => {
            if (skill) {
                let skillItemConfig = gameConfigHandle.configByID[skill.tid]
                let altarSkillAttris = altarConfig.altarSkillAttris[skillItemConfig.altarSkillIndex]
                utils.entries(altarSkillAttris).map(([attriName, values]) => {
                    let value = utils.getIncValues(values, skill.lvl)
                    fighter.attris[attriName] += value
                })
            }
        })

    }


    calcGemTeamAttris(player: PlayerModel, partner: PartnerModel, fighter: FighterModel) {
        let gemTeamAttriConfig = gameConfigHandle.gameConfig.gem.gemTeamAttris
        let attriLvlupConfig = gameConfigHandle.gameConfig.attriLvlup
        let gemTeamLvl = player.gemTeamLvl
        gemTeamAttriConfig.forEach((attri, i) => {
            let attriName = attri.name
            if (gemTeamLvl >= attri.unlockLvl) {
                let value = partnerHandle.getAttriValues(attriName, attri.valueLvl, attriLvlupConfig.gemTeamAttriValues)
                fighter.attris[attriName] += value
            }
        })
    }

    calcSkill(player: PlayerModel, partner: PartnerModel, fighter: FighterModel) {
        // partner.skillList.forEach((skillItem, i) => {
        //     if (skillItem) {
        //         let skill = new FighterSkillModel()
        //         skill.tid = skillItem.skill.tid
        //         skill.lvl = player.skillLvls[skillItem.skill.tid] || 1
        //         skill.quality = skillItem.skill.quality
        //         skill.mp = skillHandle.calcSkillMp(skillItem)
        //         skill.aSkills = []
        //         skillItem.skill.aSkillIDs.forEach((aSkillItem) => {
        //             skill.aSkills.push([aSkillItem.skillID, aSkillItem.lvl2 || aSkillItem.lvl])
        //         })
        //         if (partner.lskillList[i]) {
        //             let id = partner.lskillList[i]
        //             skill.aSkills.push([partner.lskillList[i], player.lskillLvls[id] || 1])
        //         }
        //         fighter.skills[i] = skill
        //     }
        // })
        let partnerConfig = gameConfigHandle.gameConfig.partner
        let partnerItemConfig = gameConfigHandle.configByID[partner.tid]
        partnerItemConfig.skills.forEach((skillID, i) => {
            if (skillID) {
                //判断进阶技能
                if (partner.starSkillUnlock && partner.starSkillUnlock.includes(i) && partnerItemConfig.starSkills) {
                    let skill = new FighterSkillModel()
                    skill.tid = partnerItemConfig.starSkills[i]
                    skill.lvl = 1
                    fighter.skills[i] = skill
                } else if (partnerConfig.skillUnLockStarLvl[i] <= partner.starLvl) {
                    let skill = new FighterSkillModel()
                    skill.tid = skillID
                    skill.lvl = player.skillLvls[skillID] || 1
                    fighter.skills[i] = skill
                }
            }
        })
        //套装技能
        let occType = partnerItemConfig.occType
        let packageSkill = equipmentHandle.calcPackageEquipmentAttris(player, occType).skill
        if (packageSkill) {
            let skill = new FighterSkillModel()
            skill.tid = packageSkill
            skill.lvl = player.skillLvls[packageSkill] || 1
            fighter.skills.push(skill)
        }
        //传承技能
        let friendshipSkill = skillHandle.getFriendshipSkillOfHero(player, partner.tid)
        if (friendshipSkill) {
            let skill = new FighterSkillModel()
            skill.tid = friendshipSkill
            skill.lvl = 1
            fighter.skills.push(skill)
        }
    }

    calcAltarSkills(player: PlayerModel, fighter: FighterModel) {
        let altarConfig = gameConfigHandle.gameConfig.altar
        player.altar.skillList.forEach((index, i) => {
            let skillLvl = player.altar.skillsLvl[index] || 0
            let skill = new FighterSkillModel()
            let skillIDs = altarConfig.skillListLvl[index]
            let skillID = skillIDs[0]
            if (skillLvl >= 10 && skillLvl < 20) {
                skillID = skillIDs[1]
            } else if (skillLvl >= 20) {
                skillID = skillIDs[2]
            }
            skill.tid = skillID
            skill.lvl = skillLvl
            fighter.skills[i] = skill
        })
    }

    calcVip(player: PlayerModel, partner: PartnerModel, fighter: FighterModel, isMonthVip: boolean, isYearVip: boolean) {
        let { vipLvl } = playerHandle.getVip(player)
        let vipConfig = gameConfigHandle.gameConfig.vip
        vipConfig.attrisList.forEach(attriName => {
            fighter.attris[attriName] += vipConfig.vipAdd.attrisAddPer[vipLvl]
        })
        if (isMonthVip) {
            vipConfig.monthVipAttrisList.forEach(attriName => {
                fighter.attris[attriName] += vipConfig.monthVip.attrisAddPer
            })
        }
        if (isYearVip) {
            vipConfig.monthVipAttrisList.forEach(attriName => {
                fighter.attris[attriName] += vipConfig.yearVip.attrisAddPer
                // console.log(attriName,vipConfig.yearVip.attrisAddPer)
            })
        }
    }

    calcMagicMachine(player: PlayerModel, partner: PartnerModel, fighter: FighterModel) {
        // let magicMachineConfig = gameConfigHandle.gameConfig.magicMachine
        // let attrilvlupConfig = gameConfigHandle.gameConfig.attriLvlup
        // magicMachineConfig.list.forEach(id => {
        //     let lvl = player.magicMachine.listLvl[id] || 0
        //     if (lvl > 0) {
        //         let magicMachineItemConfig = gameConfigHandle.configByID[id]
        //         magicMachineItemConfig.attris.forEach((attriName, i) => {
        //             let attriValues: any = attrilvlupConfig.magicMachineAttriValues[i]
        //             let value = partnerHandle.getAttriValues(attriName, lvl, attriValues)
        //             fighter.attris[attriName] += value
        //         })
        //     }
        // })

    }

    calcPet(player: PlayerModel, partner: PartnerModel, fighter: FighterModel, battlePos: number) {
        let pet = partner.pet
        if (pet) {
            let petItemConfig = gameConfigHandle.configByID[pet.tid]
            petItemConfig.attris.forEach((attriName, i) => {
                fighter.attris[attriName] += petHandle.getPetAttriValue(pet, i)
            })
            if (petItemConfig.otherAttris) {
                Object.entries(petItemConfig.otherAttris).forEach((attriItem, i) => {
                    let attriName = attriItem[0]
                    let values = attriItem[1] as number[]
                    let value = utils.getIncValues(values, pet.pet.quality)
                    fighter.attris[attriName] += value
                })
            }
        }
        let petConfig = gameConfigHandle.gameConfig.pet
        let altarLvl = player.altarPets.altarLvl
        let altarAttriPer = utils.getIncValues(petConfig.altarAttriPer, altarLvl)
        let altarOtherAttriPer = utils.getIncValues(petConfig.altarOtherAttriPer, altarLvl)
        player.altarPets.petsInAltar.forEach((pet, i) => {
            if (pet) {
                let petItemConfig = gameConfigHandle.configByID[pet.tid]
                petItemConfig.attris.forEach((attriName, i) => {
                    fighter.attris[attriName] += petHandle.getPetAttriValue(pet, i) * (altarAttriPer / 100)
                })
                if (petItemConfig.otherAttris) {
                    Object.entries(petItemConfig.otherAttris).forEach((attriItem, i) => {
                        let attriName = attriItem[0]
                        let values = attriItem[1] as number[]
                        let value = utils.getIncValues(values, pet.pet.quality) * (altarOtherAttriPer / 100)
                        fighter.attris[attriName] += value
                    })
                }
            }
        })
    }

    calcBaseAttri(player: PlayerModel, partner: PartnerModel, fighter: FighterModel) {
        let partnerConfig = gameConfigHandle.gameConfig.partner
        utils.entries(partnerConfig.baseAttri).forEach(item => {
            let name = item[0]
            let attriItems = item[1]
            attriItems.forEach(item => {
                let attriName = item.name
                let attriValue = item.incValues
                let value = utils.getIncValues(attriValue, fighter.attris[name])
                fighter.attris[attriName] += value
            })
        })
    }

    calcTalent(player: PlayerModel, partner: PartnerModel, fighter: FighterModel, battlePos: number) {
        let talentConfig = gameConfigHandle.gameConfig.talent
        let activeIndex = player.talent.activeIndex
        if (player.talent.list[activeIndex] && player.talent.list[activeIndex][0]) {
            talentConfig.listData.forEach((list, i) => {
                list.forEach((_, j) => {
                    let node = player.talent.list[activeIndex][i][j]
                    let index = i * 6 + j
                    let nodeTypeLvl = talentConfig.nodeTypeLvl[index]
                    let attriName = (nodeTypeLvl == 0 || nodeTypeLvl == 3) ? talentConfig.listData[i][j] : node.attriName
                    let _x = j >= 3 ? 1 : -1
                    let _y = (7 - i) >= 4 ? 1 : -1

                    // if (player.talent.listType[activeIndex] + 1 == 1) {
                    //     if (_x * _y == 1) { //黑色, 对前排有效 1

                    //     } else if (_x * _y == -1) {  //白色, 对后排有效 -1

                    //     }
                    // } else if (player.talent.listType[activeIndex] + 1 == 2) {
                    //     if (_x * _y == 1) { //白色, 对后排有效 2

                    //     } else if (_x * _y == -1) {  //黑色, 对前排有效 -2

                    //     }
                    // }
                    let _flagValue = _x * _y * (player.talent.listType[activeIndex] + 1)
                    if (_flagValue == -1 || _flagValue == 2) { //白色, 对前排有效
                        if (battlePos >= 5) { //后排
                            return
                        }
                    } else if (_flagValue == 1 || _flagValue == -2) {  //黑色, 对后排有效
                        if (battlePos <= 4) { //前排
                            return
                        }
                    }
                    let values = talentConfig.attriValue[attriName]
                    let value = node.lvl * values[0] + nodeTypeLvl * node.lvl * values[1]
                    fighter.attris[attriName] += value
                })
            })
        }


    }

    calcTreasure(player: PlayerModel, partner: PartnerModel, fighter: FighterModel, battlePos: number) {
        let treasureConfig = gameConfigHandle.gameConfig.treasure
        let attriLvlupConfig = gameConfigHandle.gameConfig.attriLvlup
        let partnerItemConfig = gameConfigHandle.configByID[partner.tid]
        treasureConfig.list.map(treasureid => {
            let treasureItemInfo: TreasureItemModel = player.treasure.list[treasureid]
            if (treasureItemInfo && treasureItemInfo.lvl > 0) {
                let treasureItemConfig = gameConfigHandle.configByID[treasureid]

                //星级提升
                let star = treasureItemInfo.star || 0
                let starUp = 1 + treasureConfig.starUpAttriPer * star / 100

                treasureItemConfig.attris.map((attriItem, i, array) => {
                    let attriName = attriItem.attriName
                    let attriType = attriItem.attriType
                    if (attriType == AttriType.Attri) {
                        let value = partnerHandle.getAttriValues(attriName, treasureItemInfo.lvl, attriLvlupConfig.treasureAttriCoe) * starUp
                        fighter.attris[attriName] += value
                    } else if (attriType == AttriType.OccAttri) {
                        if (partnerItemConfig.occType == attriItem.occType) {
                            let value = partnerHandle.getAttriValues(attriName, treasureItemInfo.lvl, attriLvlupConfig.treasureAttriCoe) * starUp
                            fighter.attris[attriName] += value
                        }
                    } else if (attriType == AttriType.BattlePosAttri) {

                        if ((attriItem.battlePos == 0 && battlePos <= 4) || (attriItem.battlePos == 1 && battlePos >= 5)) {
                            let value = partnerHandle.getAttriValues(attriName, treasureItemInfo.lvl, attriLvlupConfig.treasureAttriCoe) * starUp
                            fighter.attris[attriName] += value
                        }
                    } else if (attriType == AttriType.HeroQualityAttri) {
                        if (partnerItemConfig.quality == attriItem.heroQuality) {
                            let value = partnerHandle.getAttriValues(attriName, treasureItemInfo.lvl, attriLvlupConfig.treasureAttriCoe) * starUp
                            fighter.attris[attriName] += value
                        }
                    } else if (attriType == AttriType.Adventure) {
                        let value = utils.getIncValues(attriItem.values, treasureItemInfo.lvl) * starUp
                        if (attriName == 'clickGold') {

                        } else if (attriName == 'onLineGetGold') {

                        }
                    }
                })
            }

        })
    }

    //免疫系统属性计算  训练
    calcImmune(player: PlayerModel, partner: PartnerModel, fighter: FighterModel) {
        let immuneListLvls = player.research.immuneList
        let attrilvlupConfig = gameConfigHandle.gameConfig.attriLvlup
        let researchConfig = gameConfigHandle.gameConfig.research
        let immuneList = researchConfig.immunes[player.research.immuneIndex]
        let starUpPer = researchConfig.immuneStarUpPer
        immuneList.forEach((immuneId, i) => {
            let immuneItemConfig = gameConfigHandle.configByID[immuneId]
            let lvl = immuneListLvls[i]
            let starLvl = player.research.immuneStarList[i]
            let up = 1 + starUpPer * starLvl / 100
            immuneItemConfig.attris.forEach((attriItem, index) => {
                let attriName = attriItem.attriName
                let attriType = attriItem.attriType
                let values = attriItem.values
                if (attriType == AttriType.Attri) {
                    let value = partnerHandle.getAttriValues(attriName, lvl, values) * up
                    fighter.attris[attriName] += value
                } else if (attriType == AttriType.OccAntiHurtPer) {
                    let value = partnerHandle.getAttriValues('occAntiHurtPer', lvl, values) * up
                    if (!fighter.occAntiHurtPer[attriName]) {
                        fighter.occAntiHurtPer[attriName] = 0
                    }
                    fighter.occAntiHurtPer[attriName] += value
                }
            })
        })
        // utils.entries(immuneLvls).forEach(item => {
        //     let immuneId = item[0]
        //     let lvl = item[1] as number
        //     let immuneItemConfig = gameConfigHandle.configByID[immuneId]
        //     utils.entries(immuneItemConfig.attris).forEach(it => {
        //         let attriName = it[0]
        //         let value = partnerHandle.getAttriValues(attriName, lvl, attrilvlupConfig.immuneAttriCoe[immuneItemConfig.immuneAttriCoeType])
        //         fighter.attris[attriName] += value
        //     })
        // })
    }

    calcVaccine(player: PlayerModel, partner: PartnerModel, fighter: FighterModel) { //计算病毒疫苗效果
        let researchConfig = gameConfigHandle.gameConfig.research
        utils.entries(player.research.vaccines).forEach(item => {
            let monsterIndex = item[0]
            let vaccineLv = item[1] as number
            let hurtPer = utils.getIncValues(researchConfig.vaccineHurtPer, vaccineLv)
            fighter.monsterHurtPer[monsterIndex] = hurtPer
        })
    }

    calcEquipments(player: PlayerModel, partner: PartnerModel, fighter: FighterModel) {
        let equipmentConfig = gameConfigHandle.gameConfig.equipment
        let strConfig = equipmentConfig.equipmentsStr
        let partnerItemConfig = gameConfigHandle.configByID[partner.tid]
        let attriLvlupConfig = gameConfigHandle.gameConfig.attriLvlup

        let occType = partnerItemConfig.occType
        let packageAttris = equipmentHandle.calcPackageEquipmentAttris(player, occType)//套装属性
        Object.entries(packageAttris.attris).forEach(item => {
            let name = item[0]
            let value = item[1]
            fighter.attris[name] += value
        })
        let equipmentAttris = packageAttris.equipmentAttris

        equipmentConfig.equipmentListType.forEach(equipmentType => {
            // let equipmentItem: ItemModel = partner.equipments[partner.equipmentActive][equipmentType] 
            let equipmentItem: ItemModel = player.teamEquipment.equipmentList[occType][0][equipmentType]
            if (equipmentItem) {
                let mainAttris = equipmentItem.equipment.mainAttris
                let strLvl = partner.equipmentsStr[equipmentType]

                let up = equipmentAttris['equipmentMainUpPer'] || 0
                mainAttris.forEach(attri => {
                    let name = attri.name
                    let value = attri.value * (1 + up / 100)
                    fighter.attris[name] += value

                })
                let equipmentStrAttri = equipmentConfig.equipmentListStrAttri[equipmentType]
                let strValue = partnerHandle.getAttriValues(equipmentStrAttri.attriName, strLvl, attriLvlupConfig.equipmentsStrAttriCoe)
                fighter.attris[equipmentStrAttri.attriName] += strValue
                let subAttris = equipmentItem.equipment.subAttris
                up = equipmentAttris['equipmentSubUpPer'] || 0
                subAttris.forEach(attri => {
                    let name = attri.name
                    let attriType = attri.type
                    let value = attri.value * (1 + up / 100)
                    if (attriType == AttriType.Attri) {
                        fighter.attris[name] += value
                    } else if (attriType == AttriType.SpAttri) {
                        fighter.attris[name] += value
                    } else if (attriType == AttriType.SkillHurtPer) {
                        if (!fighter.skillsHurtPer[name]) {
                            fighter.skillsHurtPer[name] = 0
                        }
                        fighter.skillsHurtPer[name] += value
                    } else if (attriType == AttriType.StatusHurtPer) {
                        if (!fighter.statusHurtPer[name]) {
                            fighter.statusHurtPer[name] = 0
                        }
                        fighter.statusHurtPer[name] += value
                    } else if (attriType == AttriType.StatusReduceHurt) {
                        if (!fighter.statusReduceHurt[name]) {
                            fighter.statusReduceHurt[name] = 0
                        }
                        fighter.statusReduceHurt[name] += value
                    } else if (attriType == AttriType.DecSkillCd) {
                        if (!fighter.skillCdPers[name]) {
                            fighter.skillCdPers[name] = 0
                        }
                        fighter.skillCdPers[name] += value
                    }

                })
                //宝石
                // for (let i = 0; i < partner.equipmentsGem[equipmentType].length; i++) {
                //     let gemItem = partner.equipmentsGem[equipmentType][i]
                //     this.calcGem(fighter, gemItem, equipmentType)
                // }
            }
            if (equipmentItem) {
                if (partner.equipmentsEnchants[equipmentType]) {
                    if (partner.equipmentsEnchants[equipmentType].mainType) {
                        fighter.attris[partner.equipmentsEnchants[equipmentType].mainType]
                            += utils.getIncValues(equipmentConfig.equipmentEnchantValuesList[partner.equipmentsEnchants[equipmentType].mainType],
                                partner.equipmentsEnchants[equipmentType].mainLvl)
                    }
                    if (partner.equipmentsEnchants[equipmentType].subType) {
                        fighter.attris[partner.equipmentsEnchants[equipmentType].subType]
                            += utils.getIncValues(equipmentConfig.equipmentEnchantValuesList[partner.equipmentsEnchants[equipmentType].subType],
                                partner.equipmentsEnchants[equipmentType].subLvl)
                    }
                }

            }
        })
    }

    calcGem(fighter: FighterModel, gemItem: ItemModel, equipmentType: EquipmentType) {
        if (gemItem) {
            let attriLvlupConfig = gameConfigHandle.gameConfig.attriLvlup
            let gemsConfig = gameConfigHandle.gameConfig.gem.gemList
            let gemItemConfig = gameConfigHandle.configByID[gemItem.tid]
            let gemAttriConfig = gemsConfig[gemItemConfig.gemType].attris[equipmentType]
            let attriName = gemAttriConfig.name
            let lvl = gemItemConfig.gemLvl
            let value = partnerHandle.getAttriValues(gemAttriConfig.name, lvl, attriLvlupConfig.gemCoe)
            fighter.attris[attriName] += value
        }
    }
}


let fighterPartnerHandle = new FighterPartnerHandle()

export default fighterPartnerHandle
